package com.leoandroid.tool.toolsbox.adapter

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.leoandroid.tool.toolsbox.bean.DriverBean
import com.leoandroid.tool.toolsbox.largeread.R
import com.leoandroid.tool.toolsbox.largeread.databinding.AdapterDriverBinding

class DriverAdapter(val context: Context,val data:List<DriverBean.Driver>): RecyclerView.Adapter<DriverAdapter.Holder>() {
    inner class Holder(itemView: View):RecyclerView.ViewHolder(itemView){

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val layout = DataBindingUtil.inflate<AdapterDriverBinding>(LayoutInflater.from(context), R.layout.adapter_driver,parent,false)
        return Holder(layout.root)
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        val dataBind:AdapterDriverBinding? = DataBindingUtil.getBinding<AdapterDriverBinding>(holder.itemView)
        dataBind?.apply {
            val driver = data[position]
            Log.d("ppppppp","driver.url>>"+driver.url)
            Glide.with(context).load(driver.url).into(imageview)
            titleTv.text = "${position+1}、"+driver.question

            when(driver.answer){
                "1" -> answerTv.text = "正确答案：A"
                "2" -> answerTv.text = "正确答案：B"
                "3" -> answerTv.text = "正确答案：C"
                "4" -> answerTv.text = "正确答案：D"
            }
            if(TextUtils.isEmpty(driver.item3) && TextUtils.isEmpty(driver.item4)){
                question3Tv.visibility = View.GONE
                question4Tv.visibility = View.GONE

                if(driver.answer.equals("1")){
                    question2Tv.visibility = View.GONE
                    question1Tv.visibility  =View.VISIBLE
                } else if(driver.answer.equals("2")){
                    question1Tv.visibility = View.GONE
                    question2Tv.visibility = View.VISIBLE
                }
            } else {
                question3Tv.visibility = View.VISIBLE
                question4Tv.visibility = View.VISIBLE
            }
            question1Tv.text = "A. "+driver.item1
            question2Tv.text = "B. "+driver.item2
            question3Tv.text = "C. "+driver.item3
            question4Tv.text = "D. "+driver.item4
            jiexiTv.text = "解析说明："+driver.explains
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }
}